<?php

!defined('IN_ROOT') && exit('Access Denied');

class admin_adcontrol extends base {

    function admin_adcontrol(& $get,& $post) {
        $this->base( & $get,& $post);
        $this->load('ad');
    }

    function ondefault($message='') {
        if(empty($message)) unset($message);
        $typelist = array('首页通栏广告','首页页尾广告','首页栏目间广告1','首页栏目间广告2','首页栏目间广告3','首页栏目右侧广告','浏览问题正文内广告','浏览分类通栏广告');
        $stylelist = array('text'=>'文字','image'=>'图片','flash'=>'Flash','code'=>'代码');
        @$page = max(1, intval($this->get[2]));
        $pagesize=$this->setting['list_default'];
        $startindex = ($page - 1) * $pagesize;
        $rownum=$this->db->fetch_total('ad',' 1=1');
        $adlist = $_ENV['ad']->get_list($startindex,$pagesize);
        $departstr=page($rownum, $pagesize, $page,"admin_ad/default");
        include template('adlist','admin');
    }

    function onadd() {
        if(isset($this->post['submit'])) {
            $title       = $this->post['title'];
            $adtype      = $this->post['type'];
            $style       = trim($this->post['style']);
            $starttime   = $this->post['starttime'];
            $endtime     = $this->post['endtime'];
            $html        = $this->post['html'];
            $textname    = $this->post['textname'];
            $texturl     = $this->post['texturl'];
            $imageurl    = $this->post['imageurl'];
            $imageadurl  = $this->post['imageadurl'];
            $imagewidth  = $this->post['imagewidth'];
            $imageheight = $this->post['imageheight'];
            $flashurl    = $this->post['flashurl'];
            $flashwidth  = $this->post['flashwidth'];
            $flashheight = $this->post['flashheight'];
            $newad = array();
            $newad['style'] = $style;
            $newad['title'] = $title;
            $type='errormsg';
            if(!$title || strlen($title) > 50) {
                $message = '广告标题填写错误，不为空，不能超过50个字!';
                include template('addad','admin');
                exit;
            }
            if('code'==$style) {
                if(!$html) {
                    $message = 'html代码不能为空!';
                    include template('addad','admin');
                    exit;
                }
                $newad['html']=$html;
            }elseif('image'==$style) {
                if(!$imageurl || !$imageadurl) {
                    $message = '图片引用地址或图片广告URL不能为空!';
                    include template('addad','admin');
                    exit;
                }
                $newad['imgurl']     = $imageurl;
                $newad['imgadurl']   = $imageadurl;
                $newad['imagewidth'] = intval($imagewidth);
                $newad['imageheight']= intval($imageheight);
            }elseif('text'==$style) {
                if(!$textname || !$texturl) {
                    $message = '文字描述或文字广告URL不能为空!';
                    include template('addad','admin');
                    exit;
                }
                $newad['textname'] = $textname;
                $newad['texturl']  = $texturl;
            }elseif('flash'==$style) {
                if(!$flashurl || !$flashwidth || !$flashheight) {
                    $message = 'flash引用地址、宽度、高度均不能为空!';
                    include template('addad','admin');
                    exit;
                }
                $newad['flashurl']    = $flashurl;
                $newad['flashwidth']  = $flashwidth;
                $newad['flashheight'] = $flashheight;
            }
            unset($type);
            $newad['html'] = $_ENV['ad']->create_html($newad);
            $_ENV['ad']->add($title,$adtype,serialize($newad),$newad['html'],$starttime,$endtime);
            $this->cache->remove('ad');
            $this->ondefault('广告添加成功！');
        }else {
            include template('addad','admin');
        }
    }

    function onedit() {
        if(isset($this->post['submit'])) {
            $title       = $this->post['title'];
            $adtype      = $this->post['type'];
            $style       = trim($this->post['style']);
            $starttime   = $this->post['starttime'];
            $endtime     = $this->post['endtime'];
            $html        = $this->post['html'];
            $textname    = $this->post['textname'];
            $texturl     = $this->post['texturl'];
            $imageurl    = $this->post['imageurl'];
            $imageadurl  = $this->post['imageadurl'];
            $imagewidth  = $this->post['imagewidth'];
            $imageheight = $this->post['imageheight'];
            $flashurl    = $this->post['flashurl'];
            $flashwidth  = $this->post['flashwidth'];
            $flashheight = $this->post['flashheight'];
            $advid       = intval($this->post['advid']);
            $newad = array();
            $newad['style'] = $style;
            $newad['title'] = $title;
            $type='errormsg';
            $ad = $_ENV['ad']->get($advid);
            if(!$title || strlen($title) > 50) {
                $message = '广告标题填写错误，不为空，不能超过50个字!';
                include template('addad','admin');
                exit;
            }
            if('code'==$style) {
                if(!$html) {
                    $message = 'html代码不能为空!';
                    include template('addad','admin');
                    exit;
                }
                $newad['html']=$html;
            }elseif('image'==$style) {
                if(!$imageurl || !$imageadurl) {
                    $message = '图片引用地址或图片广告URL不能为空!';
                    include template('addad','admin');
                    exit;
                }
                $newad['imgurl']     = $imageurl;
                $newad['imgadurl']   = $imageadurl;
                $newad['imagewidth'] = intval($imagewidth);
                $newad['imageheight']= intval($imageheight);
            }elseif('text'==$style) {
                if(!$textname || !$texturl) {
                    $message = '文字描述或文字广告URL不能为空!';
                    include template('addad','admin');
                    exit;
                }
                $newad['textname'] = $textname;
                $newad['texturl']  = $texturl;
            }elseif('flash'==$style) {
                if(!$flashurl || !$flashwidth || !$flashheight) {
                    $message = 'flash引用地址、宽度、高度均不能为空!';
                    include template('addad','admin');
                    exit;
                }
                $newad['flashurl']    = $flashurl;
                $newad['flashwidth']  = $flashwidth;
                $newad['flashheight'] = $flashheight;
            }
            unset($type);
            $newad['html'] = $_ENV['ad']->create_html($newad);
            $_ENV['ad']->update($title,$adtype,serialize($newad),$newad['html'],$starttime,$endtime,$advid);
            $this->cache->remove('ad');
            $this->ondefault('广告编辑成功！');
        }else {
            $ad = $_ENV['ad']->get(intval($this->get[2]));
            include template('addad','admin');
        }
    }

    function onremove() {
        $message='没有选择广告！';
        if(isset($this->post['submit'])) {
            $ids = implode("," , $this->post['advid']);
            $_ENV['ad']->remove_by_id($ids);
            $this->cache->remove('ad');
            $message='广告刪除成功！';
            unset($this->get);
        }
        $this->ondefault($message);
    }

    function onavailable() {
        $available = intval($this->get[3])?0:1;
        $_ENV['ad']->update_available(intval($this->get[2]),$available);
        $this->cache->remove('ad');
        $message = $available ? '广告启用成功!':'广告禁用成功!';
        unset($this->get);
        $this->ondefault($message);
    }

}
?>